import os
import shutil
import keyboard
from openpyxl.reader.excel import load_workbook
from Summary import summary
from file import find_files, csv_to_xlsx_pd, get_serial_number, check
from wb import correction, filtration


package = os.listdir()
print('项目地址https://gitee.com/littlesquid_mc/Youth_Study/tree/master')
for filename in os.listdir():
    if filename == 'scratch_file.xlsx':
        os.remove('template.xlsx')
        shutil.copy('scratch_file.xlsx', 'template.xlsx')
        os.remove('scratch_file.xlsx')
        print('异常处理完成')
    elif filename == 'scratch_error_file.xlsx':
        os.remove('error.xlsx')
        shutil.copy('scratch_error_file.xlsx', 'error.xlsx')
        os.remove('scratch_error_file.xlsx')
        print('异常处理完成')
check()
if find_files() != 'error':
    shutil.copy('template.xlsx', 'scratch_file.xlsx')
    shutil.copy('error.xlsx', 'scratch_error_file.xlsx')
    csv_to_xlsx_pd(find_files(), './input/input.xlsx')
    correction()
    filtration()
    summary()
    shutil.copy('template.xlsx', './output/' + get_serial_number() + '青年大学习.xlsx')
    print(get_serial_number() + '青年大学习.xlsx已输出于output文件夹')
    wb = load_workbook('error.xlsx')
    ws = wb['未知']
    row = ws.max_row
    if row >= 3:
        shutil.copy('error.xlsx', './output/' + get_serial_number() + '青年大学习异常人数.xlsx')
        print(get_serial_number() + '青年大学习异常人数.xlsx已输出于output文件夹')
    wb.save('error.xlsx')
    os.remove('template.xlsx')
    os.remove('error.xlsx')
    shutil.copy('scratch_file.xlsx', 'template.xlsx')
    shutil.copy('scratch_error_file.xlsx', 'error.xlsx')
    os.remove('scratch_file.xlsx')
    os.remove('scratch_error_file.xlsx')
print('程序结束,按下回车键关闭窗口')
keyboard.wait('enter')
